﻿using Net5ApiANdWpfClient.Lib.Common;
using Net5ApiANdWpfClient.Lib.Model;
using Net5ApiANdWpfClient.Lib.Model.SystemAdmin;
using Net5ApiANdWpfClient.Lib.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfClient.OutsourceManger
{
    /// <summary>
    /// ShowOutsource.xaml 的交互逻辑
    /// </summary>
    public partial class ShowOutsource : BaseUserControl
    {
        public ShowOutsource()
        {
            InitializeComponent();
            outsourcePage.PageSize = PcConfig.PageSize;
            outsourcePage.PageCurrent = 1;
            Search();
        }
        public int Search()
        {
            string os_name = outun.Text;
            string os_tel = outunf.Text;
            PagerResult<outsource> result = new OutsourceService().SearchPageOutSource(os_name,os_tel, outsourcePage.PageCurrent);
            if (result != null)
            {
                outsourcelist.DataContext = result.Data;
                outsourcePage.NMax = result.recordsTotal;
                return result.recordsTotal;
            }
            return 0;
        }
        private void SearchBtn_Click(object sender, RoutedEventArgs e)
        {
            Search();
        }

        private int OperatorPage_EventPaging(Common.EventPagingArg e)
        {
            return Search();
        }

        private void AddBtn_Click(object sender, RoutedEventArgs e)
        {
            AddOutSource add = new AddOutSource();
            add.ShowDialog();
            Search();
        }

        //编辑
        private void Edit_Click(object sender, RoutedEventArgs e)
        {
           
            outsource obj = (outsource)outsourcelist.CurrentItem;
            int result = obj.os_id;
            int os_id = Convert.ToInt32(result);
            EditOutsource editout = new EditOutsource(os_id: os_id);
            editout.ShowDialog();
            Search();
            
        }
        //删除
        private void Del_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn!=null)
            {
                MessageBoxResult dr = MessageBox.Show("确定要删除吗 ?，删除后不可恢复!");
                if (dr == MessageBoxResult.OK)
                {
                    int id = Convert.ToInt32(btn.Tag);
                    OutsourceService os = new OutsourceService();
                    os.DeleteOutsource(id);
                    MessageBox.Show("删除成功!");
                    Search();
                }
            }
        }
        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CbAllCreate_Click(object sender, RoutedEventArgs e)
        {
            CbAll(outsourcelist, cbAllCreate);

        }
        /// <summary>
        /// 反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CbInverseCreate_Click(object sender, RoutedEventArgs e)
        {
            Inverse(outsourcelist, cbInverseCreate);
        }
        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="dg"></param>
        /// <param name="cb"></param>
        void CbAll(DataGrid dg, CheckBox cb)
        {
            dg.IsEnabled = cb.IsChecked != true;

            for (int i = 0; i < dg.Items.Count; i++)
            {
                DataGridRow neddrow = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
                //获取该行的某列
                if (neddrow != null)
                {
                    FrameworkElement objElement = dg.Columns[0].GetCellContent(neddrow);
                    if (objElement != null)
                    {
                        CheckBox objChk = (CheckBox)objElement;
                        objChk.IsChecked = cb.IsChecked;
                    }
                }
            }
        }
        /// <summary>
        /// 反选
        /// </summary>
        /// <param name="dg"></param>
        /// <param name="cb"></param>
        void Inverse(DataGrid dg, CheckBox cb)
        {
            // cb.IsChecked = false;
            for (int i = 0; i < dg.Items.Count; i++)
            {
                DataGridRow neddrow = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
                //获取该行的某列
                if (neddrow != null)
                {
                    FrameworkElement objElement = dg.Columns[0].GetCellContent(neddrow);
                    if (objElement != null)
                    {
                        CheckBox objChk = (CheckBox)objElement;
                        objChk.IsChecked = !objChk.IsChecked;
                    }
                }
            }
        }
    }
}
